﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web
{
    public partial class productSupply : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(productSupply));
        }

        //获取产品分类
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetGoodsClass()
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            StringBuilder builder=new StringBuilder();
            StringBuilder builder1=new StringBuilder();
            try
            {
                //获取商品归类
                var dtClassify = GoodsClassBll.GetGoodsClassTable("GC_Level=2 and GC_Status=1 and CHARINDEX('c',GC_Flag)<>0", "GC_PID,GC_Order",
                                                                  CommonConst.DB_Name);
                foreach (DataRow row in dtClassify.Rows)
                {
                    builder.AppendFormat(
                        "<li><div class=\"catename\"><a href=\"/products/{1}-{2}-0-0.html\">{0}</a></div><div class=\"catelist\"><div class=\"bh-fl\">",
                        row["GC_Name"], row["GC_PId"], row["GC_Id"]);

                    builder1.Append("<div class=\"bh-space\"></div><div class=\"wp\"><div class=\"page2left bh-fl\"><div class=\"bh-gglei-wyl\">");
                    builder1.AppendFormat("<div class=\"bh-gglTitle-wyl\">{0}</div>", row["GC_Name"]);
                    builder1.Append("<ul class=\"bh-gglCon-wyl\">");

                    var dtClass =GoodsClassBll.GetGoodsClassTable("GC_Level=3 and GC_Status=1 and GC_PId=" + row["GC_Id"],"GC_Order",CommonConst.DB_Name);
                    int i = 0;
                    string gcId = "";
                    foreach (DataRow rowClass in dtClass.Rows)
                    {
                        gcId += "," + rowClass["GC_Id"];
                        builder.AppendFormat("<dl class=\"fore{1}\"><dt><a href=\"/products/{2}-{3}-{4}-0.html\">{0}</a></dt>", rowClass["GC_Name"], i, row["GC_PId"], row["GC_Id"], rowClass["GC_Id"]);
                        builder.Append("<dd>");

                        builder1.AppendFormat("<li><a href=\"/products/{1}-{2}-{3}-0.html\"><b>.</b>{0}</a>",rowClass["GC_Name"], row["GC_PId"], row["GC_Id"], rowClass["GC_Id"]);

                        var dtInfo = GoodsInfoBll.GetGoodsInfoTable("G_Status=1 and GC_Id=" + rowClass["GC_Id"], "",CommonConst.DB_Name);
                        foreach (DataRow rowInfo in dtInfo.Rows)
                        {
                            builder.AppendFormat("<em><a href=\"/products/{1}-{2}-{3}-{4}.html\">{0}</a></em>", rowInfo["G_Name"], row["GC_PId"], row["GC_Id"], rowClass["GC_Id"], rowInfo["G_Id"]);
//                            builder1.AppendFormat("|<a href=\"/products/{1}-{2}-{3}-{4}.html\">{0}</a>",rowInfo["G_Name"], row["GC_PId"], row["GC_Id"], rowClass["GC_Id"],rowInfo["G_Id"]);
                        }

                        builder1.Append("</li>");
                        builder.Append("<div class=\"bh-clear\"></div></dd><div class=\"bh-clear\"></div></dl>");
                        i++;
                    }
                    if (gcId!="")
                    {
                        gcId = gcId.Substring(1);
                    }
                    gcId = gcId == "" ? "0" : gcId;
                    builder1.Append("</ul></div></div><div class=\"page2right bh-fr\"><div class=\"bh-chpt-wyl\">");
                    //查询最新的8个产品
                    Dictionary<string, object> product = new Dictionary<string, object>();
                    product.Add("more", "GC_Id in(" + gcId + ") and CHARINDEX('c',Product_Flag)<>0");
                    var dtProduct = ProductInfoBll.GetProductInfoTable("top 8 *", product, "", CommonConst.DB_Name,true);
                    StringBuilder proBuilder=new StringBuilder();
                    foreach (DataRow rowpro in dtProduct.Rows)
                    {
                        proBuilder.Append("<dl class=\"bh-chpt-dl-wyl\">");
                        proBuilder.AppendFormat("<dt><a target=\"_blank\"  href=\"/product/{0}.html\"><img src=\"{1}\" /></a></dt>", EncryptHelper.EncryptString(rowpro["Product_Id"].ToString()), rowpro["Product_Picture"]);
                        proBuilder.AppendFormat("<dd><span><a target=\"_blank\"  href=\"/product/{0}.html\">{1}</a></span>", EncryptHelper.EncryptString(rowpro["Product_Id"].ToString()), rowpro["Product_Title"]);
                        proBuilder.Append("</dd></dl>");
                    }
                    builder1.Append(proBuilder);
                    builder1.Append("<div class=\"bh-clear\"></div></div></div><div class=\"bh-clear\"></div></div><div class=\"bh-space\"></div>");

                    builder.Append("</div>");
                    //促销活动
//                    builder.Append("<div class=\"bh-fr\"><h6>推荐品牌</h6><div><a href=\"#\">XXXX旗舰店</a></div>");
//                    builder.Append("<h6>促销活动</h6><div><a href=\"#\">XXXX旗舰店</a></div></div>");

                    builder.Append("<div class=\"bh-clear\"></div>");
                    builder.Append("</div>");
                    builder.Append("</div></li>");
                }
                jsonResult.Add("isreload",true);
                jsonResult.Add("msg",builder.ToString());
                jsonResult.Add("msg1",builder1.ToString());
            }
            catch (Exception ex)
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //极力推荐
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetRecProduct()
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> product = new Dictionary<string, object>();
            try
            {
                product.Add("more", "CHARINDEX('a',Product_Flag)<>0");
                var dtProduct = ProductInfoBll.GetProductInfoTable("top 8 *", product, "", CommonConst.DB_Name,false);
                StringBuilder builder = new StringBuilder();
                foreach (DataRow row in dtProduct.Rows)
                {
                    builder.AppendFormat("<a target=\"_blank\" title=\"{2}\" href=\"/product/{0}.html\"><img src=\"{1}\" /></a>", EncryptHelper.EncryptString(row["Product_Id"].ToString()), row["Product_Picture"], row["Product_Title"]);
                }
                builder.Append("<div class=\"bh-clear\"></div>");
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", builder.ToString());
            }
            catch (Exception ex)
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
